package com.cicadamob.contact.api.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cicadamob.contact.api.Utils.StringUtil;
import com.cicadamob.contact.api.Utils.BizApi.SearchContactsUtil;
import com.cicadamob.contact.api.group.dao.Group_Db_Dao;

import android.content.Context;
import android.os.Handler;

public class SearchRunnable implements Runnable {
	private Context mContext;
	private Handler myHandler;
	private int type;
	private String keyWorke;
	private ICallBack<Map<Integer,ArrayList<Contact>>> callBack;
	private String groupId;
	private boolean onlyInstalled;
	
	public SearchRunnable(Context mContext,Handler myHandler,int type,String keyWorke,String groupId,boolean onlyInstalled,ICallBack<Map<Integer,ArrayList<Contact>>> callBack){
		this.mContext=mContext;
		this.myHandler=myHandler;
		this.type=type;
		this.keyWorke=keyWorke;
		this.callBack=callBack;
		this.groupId=groupId;
		this.onlyInstalled=onlyInstalled;
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		BaseResultModel<Map<Integer,ArrayList<Contact>>> data=doInBackground(mContext);
		if(myHandler!=null && data!=null)
		  myHandler.obtainMessage(1, data).sendToTarget();
	}

	public BaseResultModel<Map<Integer,ArrayList<Contact>>> doInBackground(Context mContext) {
		// TODO Auto-generated method stub
		switch (type) {
		case SearchContactsUtil.SEARCH_PERSON:
			ArrayList<Contact> sysList=null;
			if (onlyInstalled) {
				List<Contact> list = new ArrayList<Contact>();
				Map<String, String> installList = APPContactDao
						.getInstallPhoneByName(mContext,
								keyWorke);
				for (String key : installList.keySet()) {
					Contact cc = new Contact();
					cc.setDisplayName(installList.get(key));
					cc.setLinkPhone(key);
					cc.setInstall(1);
					;
					list.add(cc);
				}
				 sysList = (ArrayList<Contact>) list;
			} else {
				 sysList = APPContactDao.getContactsByWhere(mContext, keyWorke);
			}
			
			
			HashMap<Integer, ArrayList<Contact>> sysMap=	new HashMap<Integer, ArrayList<Contact>>();
			sysMap.put(SearchContactsUtil.SEARCH_PERSON, sysList);
			
			return new BaseResultModel<Map<Integer,ArrayList<Contact>>>(0, "查询成功",sysMap );
			
		case SearchContactsUtil.SEARCH_COMPANY_PERSON:
			ArrayList<Contact> collegeList=ContactDbHelper.getInstance().searchByKey(
					ContactDbHelper.COMPANY_PERSON_DATABASE, keyWorke, 0,
					20,onlyInstalled);
			HashMap<Integer, ArrayList<Contact>> collegeMap=	new HashMap<Integer, ArrayList<Contact>>();
			collegeMap.put(SearchContactsUtil.SEARCH_COMPANY_PERSON, collegeList);
			return new BaseResultModel<Map<Integer,ArrayList<Contact>>>(0, "查询成功",collegeMap );

		case SearchContactsUtil.SEARCH_CLIENT_PERSON:
			
			ArrayList<Contact> clientList=ContactDbHelper.getInstance().searchByKey(
					ContactDbHelper.COMPANY_CLIENT_PERSON_TABLE, keyWorke,
					0, 20,onlyInstalled);
					
			HashMap<Integer, ArrayList<Contact>> clientMap=	new HashMap<Integer, ArrayList<Contact>>();
			clientMap.put(SearchContactsUtil.SEARCH_CLIENT_PERSON, clientList);
			return new BaseResultModel<Map<Integer,ArrayList<Contact>>>(0, "查询成功",clientMap );

		case SearchContactsUtil.SEARCH_GROUP:
//			return Group_Db_Dao.getInstance(mContext).queryGroupByName(keyWorke);

		case SearchContactsUtil.SEARCH_GROUP_MEMBER:

			ArrayList<Contact> groupList= (ArrayList<Contact>) Group_Db_Dao.getInstance(mContext)
					.queryByContactForGroupSelect(keyWorke,onlyInstalled);
			
			HashMap<Integer, ArrayList<Contact>> groupMap=	new HashMap<Integer, ArrayList<Contact>>();
			groupMap.put(SearchContactsUtil.SEARCH_GROUP_MEMBER, groupList);
			
			return new BaseResultModel<Map<Integer,ArrayList<Contact>>>(0, "查询成功",groupMap );
		case SearchContactsUtil.SEARCH_ALL:

			HashMap<Integer, ArrayList<Contact>> allMap = new HashMap<Integer, ArrayList<Contact>>();
			ArrayList<Contact> result = null;

			// 搜索同事
			result = (ArrayList<Contact>) ContactDbHelper
					.getInstance().searchByKey(
							ContactDbHelper.COMPANY_PERSON_DATABASE,
							keyWorke, 0, 20,onlyInstalled);
			allMap.put(SearchContactsUtil.SEARCH_COMPANY_PERSON, result);

			// 搜索群组
			result = (ArrayList<Contact>) Group_Db_Dao.getInstance(mContext)
					.queryByContactForGroupSelect(keyWorke,onlyInstalled);
			allMap.put(SearchContactsUtil.SEARCH_GROUP_MEMBER, result);

			// 搜索个人
			if (onlyInstalled) {
				List<Contact> list = new ArrayList<Contact>();
				Map<String, String> installList = APPContactDao
						.getInstallPhoneByName(mContext,
								keyWorke);
				for (String key : installList.keySet()) {
					Contact cc = new Contact();
					cc.setDisplayName(installList.get(key));
					cc.setLinkPhone(key);
					cc.setInstall(1);
					list.add(cc);
				}
				result = (ArrayList<Contact>) list;
			} else {
				result = APPContactDao.getContactsByWhere(mContext, keyWorke);
			}
			allMap.put(SearchContactsUtil.SEARCH_PERSON, result);

			// 搜索客户
			result = (ArrayList<Contact>) ContactDbHelper
					.getInstance().searchByKey(
							ContactDbHelper.COMPANY_CLIENT_PERSON_TABLE,
							keyWorke, 0, 20,onlyInstalled);
			allMap.put(SearchContactsUtil.SEARCH_CLIENT_PERSON, result);
			return new BaseResultModel<Map<Integer,ArrayList<Contact>>>(0, "查询成功",allMap );
			
		case SearchContactsUtil.SEARCH_GROUPID_MEMBER:
			HashMap<Integer, ArrayList<Contact>> groupIdMap = new HashMap<Integer, ArrayList<Contact>>();
			ArrayList<Contact> groupMemberList =null;
			if ( !StringUtil.isEmpty(groupId)) {
				Group_Db_Dao groupDB1 = Group_Db_Dao.getInstance(mContext);
				 groupMemberList =  (ArrayList<Contact>) groupDB1
						.queryByKeywordAndGroupId(groupId, keyWorke);
				
				groupIdMap.put(SearchContactsUtil.SEARCH_GROUPID_MEMBER, groupMemberList);
				return new BaseResultModel<Map<Integer,ArrayList<Contact>>>(0, "查询成功",groupIdMap );
			} else {
				Group_Db_Dao groupDB1 = Group_Db_Dao.getInstance(mContext);
				groupMemberList= (ArrayList<Contact>) groupDB1.queryByContact(keyWorke);
				
				groupIdMap.put(SearchContactsUtil.SEARCH_GROUPID_MEMBER, groupMemberList);
				return new BaseResultModel<Map<Integer,ArrayList<Contact>>>(0, "查询成功",groupIdMap );
			}

		}
		return null;
	}
	
}
